package ch.jamme.factory;

import java.text.*;
import java.util.*;

import ch.jamme.conf.def.*;
import ch.jamme.model.rel1.*;

public class ModelFactory {

	public static Customer createCustomerForTest(boolean useProxy) {
		return createCustomerForTest(useProxy, 1);
	}
	
	public static Customer createCustomerForTest(boolean useProxy, int orderNr) {
		Customer customer1 = ModelFactory.createCustomer(1L, "Customer Name", new ArrayList<Order>());

		// shop
		Shop shop = new Shop();
		shop.setId(1L);
		shop.setName("Shop Name");
		List<Shop> shops = new ArrayList<Shop>();
		shops.add(shop);

		// items
		Item item1 = ModelFactory.createItem(1L, "Code 1", shops);
		Item item2 = ModelFactory.createItem(2L, "Code 2", shops);
		if (useProxy) {
			item1 = ItemProxy.proxy(item1);
			item2 = ItemProxy.proxy(item2);
		}

		// order1
		for (int i = 1; i <= orderNr; i++) {
			Order order1 = ModelFactory.createOrder((long)i, (long)i, customer1, shop);
			List<Order> orders = customer1.getOrders();
			orders.add(order1);
			shop.setOrders(orders);

			// orderLines
			OrderLine[] orderLines = new OrderLine[2];
			orderLines[0] = ModelFactory.createOrderLine(11L * i, order1, item1, 1 * i, 1 * i, OrderLine.Type.NORMAL);
			orderLines[1] = ModelFactory.createOrderLine(2L * i, order1, item2, 2 * i, 2 * i, OrderLine.Type.EXPRESS);
			order1.setOrderLines(orderLines);
		}

		return customer1;
	}

	public static Customer createCustomer(String name,List<Order> orders) {
		return createCustomer(-1L,name,orders);
	}

	public static Customer createCustomer(Long id,String name,List<Order> orders) {
		Customer customer = new Customer();
		customer.setId(id);
		customer.setName(name);
		customer.setOrders(orders);
		return customer;
	}

	public static Order createOrder(Long orderNr,Customer customer,Shop shop) {
		return createOrder(-1L,orderNr,customer,shop);	
	}

	public static Order createOrder(Long id, Long orderNr, Customer customer, Shop shop) {
		Order order = new Order();
		order.setCustomer(customer);
		order.setId(id);
		order.setOrderNr(orderNr);
		order.setShop(shop);
		try {
			order.setOrderDate(DefaultObjectToStringConvertor.DEFAULT_DATE_FORMAT.parse("18-03-1974 14:10:00.000"));
		} catch (ParseException e) { }
		return order;
	}

	public static OrderLine createOrderLine(Long id, Order order, Item item, int lineNr, int quantity, OrderLine.Type type){
		OrderLine orderLine = new OrderLine();
		orderLine.setId(id);
		orderLine.setItem(item);
		orderLine.setLineNr(lineNr);
		orderLine.setOrder(order);
		orderLine.setQuantity(quantity);
		orderLine.setType(type);
		return orderLine;
	}

	public static Item createItem(Long id, String code, List<Shop> shops) {
		ItemImpl item = new ItemImpl();
		item.setCode(code);
		item.setId(id);
		item.setShops(shops);
		item.setNames(new String[] { "item id #" + id, "item code: " + code });
		return item;
	}

	public static Shop createShop(Long id, String name, List<Order> orders) {
		Shop shop = new Shop();
		shop.setId(id);
		shop.setName(name);
		shop.setOrders(orders);
		return shop;
	}
}
